<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"  xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/myconfig/layui/css/layui.css}" href="../static/myconfig/layui/css/layui.css" media="all">
    <style>
        body{margin: 20px 20px 20px 0;}
        .demo-carousel{height: 200px; line-height: 200px; text-align: center;}
    </style>
</head>
<body>
<form class="layui-form" action="" lay-filter="updateForm">
    <input type="hidden" id="businessProjectId" name="businessProjectId" value=""/>
    <div class="layui-form-item">
        <label class="layui-form-label">项目名</label>
        <div class="layui-input-block">
            <input disabled="disabled"  type="text" lay-filter="projectName" name="projectName" lay-verify="required" placeholder="请输入项目名" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">项目内容</label>
        <div class="layui-input-block">
            <textarea disabled="disabled"  placeholder="请输入项目内容" class="layui-textarea" lay-verify="required" name="projectContent"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">所属区县</label>
        <div class="layui-input-inline">
            <select disabled="disabled"  id="areaName" lay-filter="areaName" lay-verify="required">
                <option value="">--所属区--</option>
                <option value="1">南山区</option>
                <option value="2">城中区</option>
                <option value="3">鱼峰区</option>
                <option value="4">柳南区</option>
                <option value="5">柳北区</option>
                <option value="6">柳江区</option>
            </select>
        </div>
        <div class="layui-input-inline">
            <select disabled="disabled"  id="businessProjectCountyId" name="businessProjectCountyId" lay-verify="required">
                <option value="" >--所属县--</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">所属行业</label>
        <div class="layui-input-block">
            <select disabled="disabled"  id="industryTypeId" name="industryTypeId" lay-filter="aihao" lay-verify="required">
                <option value="">--行业--</option>
                <option value="1">养殖业</option>
                <option value="2">畜牧业</option>
                <option value="3">房地产业</option>
                <option value="4">旅游业</option>
                <option value="5">服务业</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">项目状态</label>
        <div class="layui-input-block">
            <select id="projectStatusId" name="projectStatusId" lay-filter="projectStatusId" lay-verify="required">
                <option value="">--项目状态--</option>
                <option value="1">立项状态</option>
                <option value="2">项目执行</option>
                <option value="3">项目完工</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">审核状态</label>
        <div class="layui-input-block">
            <select id="checkStatusId" name="checkStatusId" lay-filter="aihao" lay-verify="required">
                <option value="">--审核状态--</option>
            </select>
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">创建时间</label>
        <div class="layui-input-inline">
            <input disabled="disabled"  type="text" name="projectTime" id="date" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" style="margin-top: 10px;">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="demo1">立即提交</button>
        </div>
    </div>
</form>
<script th:src="@{/myconfig/layui/layui.js}" src="../static/myconfig/layui/layui.js"></script>

<script>
    layui.use(['form', 'layedit', 'laydate'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate
            ,$ = layui.$;

        var checkStatus = parent.businessProjectId;

        console.log(checkStatus);
        //日期
        laydate.render({
            elem: '#date'
        });

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义验证规则
        form.verify({
            title: function(value){
                if(value.length < 5){
                    return '标题至少得5个字符啊';
                }
            }
            ,pass: [
                /^[\S]{6,12}$/
                ,'密码必须6到12位，且不能出现空格'
            ]
            ,content: function(value){
                layedit.sync(editIndex);
            }
        });

        form.on('select(projectStatusId)',function (data) {
            var projectStatusId = data.value;
            var str = "";
            if (projectStatusId == 1){
                str = "<option value=\"\">--审核状态--</option><option value=\"1\">等待审批</option><option value=\"3\">审批驳回</option><option value=\"4\">等待上级审批</option>";
                $("select[name=checkStatusId]").html(str);
            }  else {
                str = "<option value=\"2\">通过审批</option>";
                $("select[name=checkStatusId]").html(str);
            }
            form.render('select');
        })

        form.on('select(areaName)', function(data){
            var areaId = data.value;
            $.ajax({
                url:'businessProject/getCountyName'
                ,data:{areaId:areaId}
                ,success:function (data) {
                    var str = "<option value=\"\" >--所属县--</option>";
                    for (var i = 0;i < data.length; i++){
                        str += "<option value='"+data[i].countyId+"' >"+data[i].countyName+"</option>";
                    }
                    $("select[name=businessProjectCountyId]").html(str);
                    form.render('select');
                }
            })
        })

        //监听提交
        form.on('submit(demo1)', function(data){
            var businessProject = data.field;
            $.ajax({
                url:'businessProject/update'
                ,data:businessProject
                ,success:function (result) {
                    if (result.code == 200){
                        layer.msg("更新成功",{
                            icon:6
                        })
                        setTimeout("freshClose()",1000);
                    } else{
                        layer.msg("更新失败"),{
                            icon:5
                        }
                    }
                }
            })
            return false;
        });

    });

    function init(businessProjectId) {
        // alert(businessProjectId + ";" + projectName+ ";" +projectContent+ ";" +projectTime+ ";" +countyName+ ";" +industryName+ ";" +projectStatusName+ ";" +statusName);
        layui.use(['form', 'layedit', 'laydate'], function(){
            var form = layui.form,
                $ = layui.$;

            $.ajax({
                url:'businessProject/initUpdateForm'
                ,data:{businessProjectId:businessProjectId}
                ,success:function (BP) {
                    form.val('updateForm',{
                        "businessProjectId":BP.businessProjectId
                        ,"projectName":BP.projectName
                        ,"projectContent":BP.projectContent
                        ,"projectTime":BP.projectTime
                        ,"countyName":BP.countyName
                        ,"projectStatusName":BP.projectStatusId
                        ,"statusName":BP.checkStatusId
                    })
                    $("#areaName").val(BP.areaId);
                    $.ajax({
                        url:'businessProject/getCountyName'
                        ,data:{areaId:BP.areaId}
                        ,success:function (data) {
                            var str = "";
                            for (var i = 0;i < data.length; i++){
                                if (data[i].countyId == BP.businessProjectCountyId){
                                    str = "<option value='"+data[i].countyId+"' >"+data[i].countyName+"</option>" + str;
                                }
                                str += "<option value='"+data[i].countyId+"' >"+data[i].countyName+"</option>";
                            }
                            $("select[name=businessProjectCountyId]").html(str);
                            form.render('select');
                        }
                    })
                    $("#industryTypeId").val(BP.industryTypeId);
                    $("#projectStatusId").val(BP.projectStatusId);
                    var str = "";
                    if (BP.projectStatusId == 1){
                        str = "<option value=\"\">--审核状态--</option><option value=\"1\">等待审批</option><option value=\"3\">审批驳回</option><option value=\"4\">等待上级审批</option>";
                        $("select[name=checkStatusId]").html(str);
                    }  else {
                        str = "<option value=\"2\">通过审批</option>";
                        $("select[name=checkStatusId]").html(str);
                    }
                    $("#checkStatusId").val(BP.checkStatusId);
                    form.render('select');
                }
            })
        })
    }
    function freshClose(){
        window.parent.location.reload();//刷新父页面
        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        parent.layer.close(index)
    }
</script>
</body>
</html>